package com.jsle.financial.service;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import com.jsle.financial.dao.UserDao;
import com.jsle.financial.domain.User;
import com.jsle.financial.domain.User_UserGroups;
import com.jsle.financial.manager.UserManager;
import com.jsle.financial.util.Constants;
import com.jsle.financial.vo.Page;
import com.jsle.financial.vo.Result;
/**
 * UserService业务逻辑处理类
 * @author wisdying
 * @date 2012-10-27
 */
@Service
public class UserServiceImpl implements UserService{

	private Log log = LogFactory.getLog(UserServiceImpl.class);
	
	@Resource
	private UserManager userManager;
	@Resource
	private UserDao userDao;
	
	/**
	 * 新增一用户
	 * @param user
	 * @return
	 */
	public Result addUser(User user){
		Result result = new Result();
		try {
			result = userManager.addUser(user);
		} catch (Exception e) {
			result.setResultCode(Constants.Service.FC_RESULT_INSERT_SERVICE_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Service.FC_RESULT_INSERT_SERVICE_ERROR_MESSAGE, "User"));
			log.error("new user error## "+ e.getMessage());
		}
		return result;
	}
	/**
	 * 删除一用户
	 * @param user
	 * @return
	 */
	public Result delUsers(User user){
		Result result = new Result();
		try {
			result = userManager.delUsers(user);
		} catch (Exception e) {
			result.setResultCode(Constants.Service.FC_RESULT_DEL_SERVICE_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Service.FC_RESULT_DEL_SERVICE_ERROR_MESSAGE, "User"));
			log.error("del user error## "+ e.getMessage());
		}
		return result;
	}
	/**
	 * 更新一用户信息
	 * @param user
	 * @return
	 */
	public Result updateUser(User user){
		Result result = new Result();
		try {
			result = userManager.updateUser(user);

		} catch (Exception e) {
			result.setResultCode(Constants.Service.FC_RESULT_UPDATE_SERVICE_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Service.FC_RESULT_UPDATE_SERVICE_ERROR_MESSAGE, "User"));
			log.error("update user error## "+ e.getMessage());
		}
		return result;
	}
	
	/**
	 * 根据登录账号获取一用户信息
	 * @param loginName
	 * @return
	 */
	public Result queryUserByLoginName(User user){
		Result result = new Result();
		try {
			result = userDao.queryUserByLoginName(user);
		} catch (Exception e) {
			result.setResultCode(Constants.Service.FC_RESULT_QUERY_SERVICE_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Service.FC_RESULT_QUERY_SERVICE_ERROR_MESSAGE, "User"));
			log.error("query user error## "+ e.getMessage());
		}
		return result;
	}
	
	/**
	 *分页查询
	 */
	public Result queryUsersByPage(Page page,String loginName){
		Result result = new Result();
		try {
			result = userDao.queryUsersByPage(page,loginName);
		} catch (Exception e) {
			result.setResultCode(Constants.Service.FC_RESULT_QUERY_SERVICE_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Service.FC_RESULT_QUERY_SERVICE_ERROR_MESSAGE, "Query Users by page"));
			log.error("query users by page error## "+ e.getMessage());
		}
		return result;
	}
	
	/**
	 * 用户配置用户组
	 * @param uug
	 * @return
	 */
	public Result addUserAndUserGroups(User_UserGroups uug){
		Result result = new Result();
		try {
			result = userDao.addUserAndUserGroups(uug);
		} catch (Exception e) {
			result.setResultCode(Constants.Service.FC_RESULT_QUERY_SERVICE_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Service.FC_RESULT_QUERY_SERVICE_ERROR_MESSAGE, "User"));
			log.error("add User_UserGroups error## "+ e.getMessage());
		}
		return result;
	}
	/**
	 * 根据申请人获取流程审核人员列表
	 * @param user
	 * @author wisdying
	 * @return
	 */
	public Result queryAssessors(User user){
		Result result = new Result();
		try {
			result = userDao.queryAssessors(user);
		} catch (Exception e) {
			result.setResultCode(Constants.Service.FC_RESULT_QUERY_SERVICE_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Service.FC_RESULT_QUERY_SERVICE_ERROR_MESSAGE, "Query Users by page"));
			log.error("queryAssessors by page error## "+ e.getMessage());
		}
		return result;
	}
	
	/**
	 * 根据Id获取用户信息
	 * @param userId
	 * @author wisdying
	 * @return
	 */
	public Result queryUserById(Integer userId){
		Result result = new Result();
		try {
			result = userDao.queryUserById(userId);
		} catch (Exception e) {
			result.setResultCode(Constants.Service.FC_RESULT_QUERY_SERVICE_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Service.FC_RESULT_QUERY_SERVICE_ERROR_MESSAGE, "Query Users by page"));
			log.error("queryUserById by page error## "+ e.getMessage());
		}
		return result;
	}
	/**
	 * 根据申请人获取流程审批人员列表
	 * @param user
	 * @author wisdying
	 * @return
	 */
	public Result queryApproves(User user) {
		Result result = new Result();
		try {
			result = userDao.queryApproves(user);
		} catch (Exception e) {
			result.setResultCode(Constants.Service.FC_RESULT_QUERY_SERVICE_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Service.FC_RESULT_QUERY_SERVICE_ERROR_MESSAGE, "Query Users by page"));
			log.error("queryApproves by page error## "+ e.getMessage());
		}
		return result;
	}
	
	/**
	 * 删除用户配置用户组
	 * @param uug
	 * @return
	 */
	public Result delUserAndUserGroups(User_UserGroups uug){
		Result result = new Result();
		try {
			result = userDao.delUserAndUserGroups(uug);
		} catch (Exception e) {
			result.setResultCode(Constants.Service.FC_RESULT_QUERY_SERVICE_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Service.FC_RESULT_QUERY_SERVICE_ERROR_MESSAGE, "delUserAndUserGroups"));
			log.error("delUserAndUserGroups error## "+ e.getMessage());
		}
		return result;
	}
	
	/**
	 * 查询某个用户配置的用户组
	 * @param uug
	 * @return
	 */
	public Result selUserAndUserGroups(User_UserGroups uug){
		Result result = new Result();
		try {
			result = userDao.selUserAndUserGroups(uug);
		} catch (Exception e) {
			result.setResultCode(Constants.Service.FC_RESULT_QUERY_SERVICE_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Service.FC_RESULT_QUERY_SERVICE_ERROR_MESSAGE, "selUserAndUserGroups"));
			log.error("selUserAndUserGroups error## "+ e.getMessage());
		}
		return result;
	}
}
